
***************************************************************************************************************
/*
THIS DO-FILE:
Gets health and death transitions (inputs into model)
*/
***************************************************************************************************************

use "my_sample_long.dta", clear
merge 1:1 hhidpn wave using "imputed_kid_assets.dta", nogen

keep if cohort>=1 & cohort<=5 // dropping cohort 6 (not enough people survives)

rename assets passets

sort hhidpn wave
by hhidpn: gen lcareneed=careneed[_n-1]

*turn age into a bi-yearly thing
gen agebi=2*ceil(age/2)

*** CARE ***
preserve 
mlogit careneed i.lcareneed##c.agebi c.agebi#c.agebi c.agebi#c.agebi#c.agebi ///
	            c.PIpctile c.PIpctile#c.agebi ///
			    female female#c.agebi

keep agebi lcareneed
drop if agebi>100
drop if lcareneed==.
duplicates drop
fillin agebi lcareneed
			  
*"h" is today's health (careneed), "g" is lagged health (careneed)
forval h=2/3 {			  
	gen ageshift`h'=  _b[`h':_cons] ///
					+(_b[`h':agebi]*agebi) ///
					+(_b[`h':c.agebi#c.agebi]*agebi*agebi) ///
					+(_b[`h':c.agebi#c.agebi#c.agebi]*agebi*agebi*agebi)
	gen PIshift`h'=_b[`h':PIpctile]+_b[`h':c.PIpctile#c.agebi]*agebi 
	gen femaleshift`h'=_b[`h':female]+_b[`h':1.female#c.agebi]*agebi
	gen healthshift`h'=0
    forval g=2/3 {
		replace healthshift`h'=_b[`h':`g'.lcareneed]+_b[`h':`g'.lcareneed#c.agebi]*agebi if lcareneed==`g'
	}
}
drop _fillin
sort agebi lcareneed
outsheet using "healthtransitions.out", nonames replace
restore
**************

*** DEATH  ***
preserve
sort hhidpn wave
cap drop firstdeath
by hhidpn: gen firstdeath=1 if inw==0 & inw[_n-1]==1
keep if inw==1 | firstdeath==1
gen dead=(firstdeath==1)
drop firstdeath
by hhidpn: replace age=age[_n-1]+2 if dead==1 & age==.
by hhidpn: replace agebi=agebi[_n-1]+2 if dead==1 & agebi==.

logit dead i.lcareneed##c.agebi c.agebi#c.agebi c.agebi#c.agebi#c.agebi ///
	       c.PIpctile c.PIpctile#c.agebi ///
		   female female#c.agebi

keep agebi lcareneed
drop if agebi>100
drop if lcareneed==.
duplicates drop
fillin agebi lcareneed

gen ageshift=_b[_cons]+_b[agebi]*agebi+_b[c.agebi#c.agebi]*agebi*agebi+_b[c.agebi#c.agebi#c.agebi]*agebi*agebi*agebi
gen PIshift=_b[PIpctile]+_b[c.PIpctile#c.agebi]*agebi 
gen femaleshift=_b[female]+_b[1.female#c.agebi]*agebi
gen 	healthshift=0
replace healthshift=_b[2.lcareneed]+_b[2.lcareneed#c.agebi]*agebi if lcareneed==2
replace healthshift=_b[3.lcareneed]+_b[3.lcareneed#c.agebi]*agebi if lcareneed==3

drop _fillin
sort agebi lcareneed

outsheet using "deathtransitions.out", nonames replace
restore
***************************************************************************************************************
